package com.more.offer.algorithm.leetcode.lcode03;

/**
 * @Author: yinpeng
 * @Date: 2019/9/25 14:58
 * @Description: TODO
 **/
public class Solution_03 {
	public static int lengthOfLongestSubstring(String s) {
		int i = 0;
		int flag = 0;
		int length = 0;
		int result = 0;
		while (i < s.length()) {
			int pos = s.indexOf(s.charAt(i),flag);
			if (pos < i) {
				if (length > result) {
					result = length;
				}
				//判断不重复字符串长度是否大于pos后的字符串长度，大于直接返回。
				if (result >= s.length() - pos - 1) {
					return result;
				}
				//计算长度
				length = i - pos - 1;
				flag = pos + 1;
			}
			length++;
			i++;
		}
		return length;
	}

	public static void main(String[] args) {
		String s = "ABCBCABCBB";
		int i = lengthOfLongestSubstring(s);
		System.out.println(i);
	}
}
